package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.AES;
import com.ocom.common.utils.MD5;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoPersonnelInfo;
import com.ocom.payhandle.service.PersonnelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonnelInfoServiceImpl extends ServiceImpl<IDaoPersonnelInfo, EntityPersonnelInfo> implements PersonnelInfoService {



    //personnelInfo 持久层
    @Autowired
    private IDaoPersonnelInfo personnelInfo;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    @Override
    public EntityPersonnelInfo selectPersonnel(Long companyId, Long pId) {
        QueryWrapper<EntityPersonnelInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("dept_id","p_code","p_name","status","p_photo_url","p_photo_url2");
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        return personnelInfo.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID")
    @Override
    public EntityPersonnelInfo selectPerInfoName(Long companyID, Integer pId) {
        QueryWrapper<EntityPersonnelInfo> query = Wrappers.query();
        query.select("p_name");
        query.eq("com_id",companyID);
        query.eq("p_id",pId);
        query.eq("status",0);
        return personnelInfo.selectOne(query);
    }

    //验证个人密码（verifyPassword）
    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    public EntityPersonnelInfo verifyPassword(Long companyId, Long pId) {
        QueryWrapper<EntityPersonnelInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("p_phone");
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityPersonnelInfo getPersonByPhone(Long companyId, String phone) {
        String key = MD5.MD5Encode(companyId.toString(), "utf8");
        String encryPhone = AES.encrypt(phone, key);
        QueryWrapper<EntityPersonnelInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("status",0);
        query.eq("p_phone",phone);
        query.orderByDesc("update_time");
        query.last("limit 1");
        return this.getOne(query);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB)
    public EntityPersonnelInfo addPerson(Long companyId, EntityPersonnelInfo entityPersonnelInfo ) {

        entityPersonnelInfo.setDeptId(0l);
        String key = MD5.MD5Encode(companyId.toString(), "utf8");
        String encryPhone = AES.encrypt(entityPersonnelInfo.getPPhone(), key);
        entityPersonnelInfo.setEncryPhone(encryPhone);

        QueryWrapper<EntityPersonnelInfo> queryWrapper =new QueryWrapper();
        queryWrapper.eq("status",0);
        queryWrapper.eq("p_code",entityPersonnelInfo.getPCode());
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        EntityPersonnelInfo entityPersonnelInfo1 = this.getOne(queryWrapper);
        if(entityPersonnelInfo1!=null){
            entityPersonnelInfo.setPId(entityPersonnelInfo1.getPId());
            this.updateById(entityPersonnelInfo);
            return entityPersonnelInfo1;
        }
        Boolean df =  this.save(entityPersonnelInfo);
        if(df) return entityPersonnelInfo;
        return  null;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    public EntityPersonnelInfo getOnePersonInfo(Long comId, Long pId) {
        QueryWrapper<EntityPersonnelInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("p_name , p_code , p_sex, p_phone,status");
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("status",0);
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityPersonnelInfo getPersonByPCode(Long companyId, String pCode) {
        QueryWrapper<EntityPersonnelInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("status",0);
        query.eq("p_code",pCode);
        query.orderByDesc("update_time");
        query.last("limit 1");
        return this.getOne(query);
    }

}
